﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Infragistics.WebUI.UltraWebGrid;
using ToolApplicationDataSetTableAdapters;

public partial class ToolApplication : System.Web.UI.Page
{
    ToolApplicationDataSet ds = new ToolApplicationDataSet();
    protected DataTable tblToolsList = new DataTable();

    protected Guid tsID = System.Guid.NewGuid();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            SetData();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        DataRow dr;
        // find or create a new row to hold the data
            dr = this.ds.ToolSheet.NewRow();

        if (dr != null)
        {
            dr["ToolSheetID"] = this.tsID.ToString();
            dr["ConstructionCorp"] = this.TxtConstructionCorp.Text.ToString();
            dr["ProposeDate"] = DateTime.Now.ToShortDateString();
            dr["StartDate"] = this.StartDate.Value.ToString();
            dr["FinishDate"] = this.FinishDate.Value.ToString();
            dr["ProposerID"] = Membership.GetUser().ProviderUserKey.ToString();
            dr["DeptAuditStatus"] = "待批";
            dr["AuditStatus"] = "待批";
        }
        this.ds.ToolSheet.Rows.Add(dr);
        using (ToolSheetTableAdapter adapter = new ToolSheetTableAdapter())
            adapter.Update(dr);

        int i = 1;
        foreach (UltraGridRow row in UltraWebGrid1.Rows)
        {
            dr = this.ds.ToolList.NewRow();

            dr["ToolSheetID"] = this.tsID.ToString();
            dr["ToolsSerial"] = i;
            dr["ToolsName"] = row.Cells[3].ToString();
            dr["ToolsQuantity"] = row.Cells[4].Value;
            this.ds.ToolList.Rows.Add(dr);
            i++;
        }
        using (ToolListTableAdapter adapter1 = new ToolListTableAdapter())
            adapter1.Update(ds);


            
    }

//行更新
protected void UltraWebGrid1_UpdateRow(object sender, Infragistics.WebUI.UltraWebGrid.RowEventArgs e)
{
    //if (e.Row.Cells[1].Value == null)
    //{
    //    //课程名不能为空 
    //    RegisterStartupScript("message", "<script language='javascript' defer>alert('课程名称不能为空！');</script>");
    //}
    //else
    //{
    //    SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["smsConnectionString"].ConnectionString);


    //    SqlDataAdapter da = new SqlDataAdapter("select * from CourseInfo where IsDeleted<>1", objConnection);
    //    DataSet ds = new DataSet();
    //    da.Fill(ds, "CourseInfo");
    //    DataTable dt = ds.Tables["CourseInfo"];

    //    SqlDataAdapter da1 = new SqlDataAdapter("select * from DepartmentInfo", objConnection);
    //    da1.Fill(ds, "DepartmentInfo");
    //    DataTable dt2 = ds.Tables["DepartmentInfo"];

    //    SqlDataAdapter da2 = new SqlDataAdapter("select staffID,DepartmentInfo.DepartmentID from DepartmentInfo,StaffInfo where DepartmentInfo.DepartmentID =StaffInfo.DepartmentID ", objConnection);
    //    da2.Fill(ds, "DepStaffInfo");
    //    DataTable dt3 = ds.Tables["DepStaffInfo"];

    //    if (e.Row.Cells[0].Value != null)
    //    {
    //        //更新

    //        objConnection.Open();
    //        //添加表CourseInfo，并返回值
    //        //创建并初始化一个objCommand对象
    //        SqlCommand objCommand = new SqlCommand();
    //        objCommand.Connection = objConnection;
    //        objCommand.CommandText = "UpdateCourse";
    //        objCommand.CommandType = CommandType.StoredProcedure;
    //        objCommand.Parameters.Clear();

    //        //写参数
    //        SqlParameter objParameter = new SqlParameter("@CourseName", SqlDbType.NVarChar);
    //        objCommand.Parameters.Add(objParameter);
    //        objParameter.Direction = ParameterDirection.Input;
    //        objParameter.Value = e.Row.Cells[1].Value;

    //        objParameter = new SqlParameter("@CourseDes", SqlDbType.NVarChar);
    //        objCommand.Parameters.Add(objParameter);
    //        objParameter.Direction = ParameterDirection.Input;
    //        objParameter.Value = e.Row.Cells[2].Value;

    //        objParameter = new SqlParameter("@CourseID", SqlDbType.NVarChar);
    //        objCommand.Parameters.Add(objParameter);
    //        objParameter.Direction = ParameterDirection.Input;
    //        objParameter.Value = e.Row.Cells[0].Value;

    //        objCommand.ExecuteNonQuery();
    //        objConnection.Close();

    //    }
    //    else
    //    {
    //        //页面验证
    //        DataRow[] rows = dt.Select("CourseName = '" + e.Row.Cells[1].Value.ToString() + " '");
    //        if (rows.Length > 0)
    //        {
    //            //数据库中已经有有效的此课程名，重名问题，提示用户输入错误
    //            RegisterStartupScript("message", "<script language='javascript' defer>alert('表中已有相同的课程！');</script>");
    //        }
    //        else
    //        {  //开始添加
    //            objConnection.Open();
    //            ////开始一个本地事务
    //            SqlTransaction trans = objConnection.BeginTransaction(IsolationLevel.Serializable);

    //            //创建并初始化一个objCommand对象
    //            SqlCommand objCommand = new SqlCommand();

    //            objCommand.Connection = objConnection;
    //            objCommand.Transaction = trans;
    //            try
    //            {

    //                //添加表CourseInfo，并返回值
    //                objCommand.CommandText = "CreateCourse";
    //                objCommand.CommandType = CommandType.StoredProcedure;
    //                objCommand.Parameters.Clear();

    //                //写参数
    //                SqlParameter objParameter = new SqlParameter("@CourseName", SqlDbType.NVarChar);
    //                objCommand.Parameters.Add(objParameter);
    //                objParameter.Direction = ParameterDirection.Input;
    //                objParameter.Value = e.Row.Cells[1].Value;

    //                objParameter = new SqlParameter("@CourseDes", SqlDbType.NVarChar);
    //                objCommand.Parameters.Add(objParameter);
    //                objParameter.Direction = ParameterDirection.Input;
    //                objParameter.Value = e.Row.Cells[2].Value;


    //                SqlParameter objOutputParameter = new SqlParameter("@CourseID", SqlDbType.Int);
    //                objCommand.Parameters.Add(objOutputParameter);
    //                objOutputParameter.Direction = ParameterDirection.Output;

    //                objCommand.ExecuteNonQuery();
    //                int nCourseID = (int)objOutputParameter.Value;


    //                for (int i = 0; i < dt2.Rows.Count; i++)
    //                {
    //                    //添加表DepCourseInfo，并返回值
    //                    objCommand.CommandText = "Departments_CreateCourse";
    //                    objCommand.CommandType = CommandType.StoredProcedure;
    //                    objCommand.Parameters.Clear();

    //                    //写参数
    //                    objParameter = new SqlParameter("@DepartmentID", SqlDbType.Int);
    //                    objCommand.Parameters.Add(objParameter);
    //                    objParameter.Direction = ParameterDirection.Input;
    //                    objParameter.Value = dt2.Rows[i].ItemArray[0];

    //                    objParameter = new SqlParameter("@CourseID", SqlDbType.Int);
    //                    objCommand.Parameters.Add(objParameter);
    //                    objParameter.Direction = ParameterDirection.Input;
    //                    objParameter.Value = nCourseID;

    //                    objOutputParameter = new SqlParameter("@DeCID", SqlDbType.Int);
    //                    objCommand.Parameters.Add(objOutputParameter);
    //                    objOutputParameter.Direction = ParameterDirection.Output;

    //                    objCommand.ExecuteNonQuery();
    //                    int nDeCID = (int)objOutputParameter.Value;

    //                    DataRow[] rs = dt3.Select("DepartmentID = '" + dt2.Rows[i].ItemArray[0] + "'");
    //                    for (int k = 0; k < rs.Length; k++)
    //                    {
    //                        objCommand.CommandText = "DepCourse_CreateStaCourse";
    //                        objCommand.CommandType = CommandType.StoredProcedure;
    //                        objCommand.Parameters.Clear();

    //                        //写参数
    //                        objParameter = new SqlParameter("@StaffID", SqlDbType.Int);
    //                        objCommand.Parameters.Add(objParameter);
    //                        objParameter.Direction = ParameterDirection.Input;
    //                        objParameter.Value = rs[k].ItemArray[0];

    //                        objParameter = new SqlParameter("@DeCID", SqlDbType.Int);
    //                        objCommand.Parameters.Add(objParameter);
    //                        objParameter.Direction = ParameterDirection.Input;
    //                        objParameter.Value = nDeCID;

    //                        objCommand.ExecuteNonQuery();
    //                    }
    //                }

    //                trans.Commit();
    //            }
    //            catch (SqlException)
    //            {
    //                //退出事务处理（撤销更改）
    //                if (trans != null)
    //                    trans.Rollback();
    //            }
    //            finally
    //            { objConnection.Close(); }
    //        }
    //    }
    //    SetData();
    //}
}

public void SetData()
{
    ConnectionStringSettings connObj = ConfigurationManager.ConnectionStrings["smsConnectionString"];
    string connectionString = connObj.ConnectionString;
    SqlConnection objConnection = new SqlConnection(connectionString);

    String query = "SELECT ToolSheetID, ToolsSerial AS 工具编号, ToolsName AS 工具名称, ToolsQuantity AS 数量 FROM ToolList where ToolSheetID = '" + tsID.ToString()+"'";

    SqlDataAdapter da = new SqlDataAdapter(query, objConnection);

    DataSet ds = new DataSet();
    da.Fill(ds, "工具单");
    DataTable dt = ds.Tables["工具单"];
    DataView view = new DataView(dt);

    this.UltraWebGrid1.DataSource = view;
    this.UltraWebGrid1.DataKeyField = "ToolsSerial";
    this.UltraWebGrid1.DataBind();
    this.UltraWebGrid1.Columns[1].Hidden = true;

}

protected void Button2_Click1(object sender, EventArgs e)
{
    SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["smsConnectionString"].ConnectionString);

    SqlDataAdapter da = new SqlDataAdapter("select * from ToolSheet", objConnection);
    SqlCommandBuilder cb = new SqlCommandBuilder(da);
    DataSet ds = new DataSet();
    da.Fill(ds, "工具单");
    DataTable dt = ds.Tables["工具单"];
    DataRow[] rows = dt.Select("ToolsSerial = " + Convert.ToInt32(this.UltraWebGrid1.DisplayLayout.ActiveRow.DataKey) + "");
    foreach (DataRow row in rows)
        row["IsDeleted"] = true;
    da.Update(dt);

    SetData();
}


}
